<template>
	<view class="content jbBG">
		<custom-nav-bar title="推荐"></custom-nav-bar>
		<view class="banner">
			<swiper :indicator-dots="true" indicator-color="rgba(255,255,255,0.5)" indicator-active-color="rgb(0,0,0)" :autoplay="true" circular>
				<swiper-item v-for="item in bannerList" :key="item._id" @click="bannerGo(item)">
					<image :src="item.picurl" mode="aspectFill"></image>
				</swiper-item>
			</swiper>
		</view>
		<view class="notice">
			<view class="left">
				<uni-icons type="sound-filled" size="20" color="#27b28c"></uni-icons>
				<text class="text">公告</text>
			</view>
			<view class="center">
				<swiper :autoplay="true" circular :duration="300" vertical>
					<swiper-item v-for="item in noticeList" :key="item._id">
						<navigator :url="`/pages/notice/detail?id=${item._id}`">
							{{item.title}}
						</navigator>
					</swiper-item>
				</swiper>
			</view>
			<view class="right">
				<uni-icons type="right" size="20"></uni-icons>
			</view>
		</view>
		<view class="select">
			<common-title>
				<template #title>
					<view>
						今日推荐
					</view>
				</template>
				<template #custom>
					<view class="custom">
						<uni-icons type="calendar" size="20" color="#27b28c"></uni-icons>
						<uni-dateformat :date="Date.now()" format="dd日"></uni-dateformat>
					</view>
				</template>
			</common-title>
			<view class="content">
				<scroll-view scroll-x >
					<view class="box" v-for="item in randomList" :key="item._id"  @click="onGoPreview(item._id)">
						<image :src="item.smallPicurl" mode="aspectFill"></image>
					</view>
				</scroll-view>
			</view>
		</view>
		<view class="theme">
			<common-title>
				 <template #title>
				 	<view>专题精选</view>
				 </template>
				 <template #custom>
				 	<navigator url="/pages/classify/classify" open-type="reLaunch" class="custom">
						More+
					</navigator>
				 </template>
			</common-title>
			<view class="content">
				<theme-item v-for="item in themeList" :key="item._id" :item="item"></theme-item>
				<theme-item :isMore="true">111</theme-item>
			</view>
		</view>
		
	</view>
</template>

<script setup>
	import { ref } from 'vue';
	import {onShareAppMessage,onShareTimeline} from '@dcloudio/uni-app'
	import {getBannerApi,getNoticeListApi,getRandomListApi,getClassifyApi} from '@/api/api.js'
	function onGoPreview(id) {
		
		uni.navigateTo({
			url:`/pages/preview/preview?id=${id}`
		})
	}
	/* 轮播图 */
	const bannerList = ref([]);
	const getBanner = async () => {
		let res = await getBannerApi();
		console.log("轮播图",res);
		bannerList.value = res.data;
	}
	getBanner();
	/* 轮播图跳转 */
	const bannerGo = (item) => {
		/* 小程序跳转 */
		if (item.target === "miniProgram") {
			console.log("小程序跳转  item",item,item.appid,item.url)
			uni.navigateToMiniProgram({
				appId:item.appid,
				path:item.url
			})
		}else if (item.target === "self") {
			uni.navigateTo({
				url:`/pages/classlist/classlist?${item.url}`
			})
		}
		
	}
	/* 随机推荐 */
	const randomList = ref([]);
	const getRandomList = async () => {
		let res = await getRandomListApi();
		console.log("推荐",res);
		randomList.value = res.data;
		/* 缓存数据 */
		console.log("随机推荐 缓存数据")
		uni.setStorageSync("storageclassList",randomList);
	}
	getRandomList();
	/* 公告列表 */
	const noticeList = ref([]);
	const getNoticeList = async () => {
		let res = await getNoticeListApi({
			select: true
		});
		console.log("公告",res);
		noticeList.value = res.data;
	}
	getNoticeList();
	/* 专题精选 */
	const themeList = ref([])
	const getClassify = async () => {
		let res = await getClassifyApi({
			select:true
		});
		console.log("精选",res);
		themeList.value = res.data;
	}
	getClassify();
	/* 分享设置 */
	onShareAppMessage((e) => {
		console.log("onShareAppMessage e",e)
		return {
			title:"分享给朋友",
			path:'/pages/index/index'
		}
	})
	/* 有问题 */
	onShareTimeline(() => {
		return {
			title:"朋友圈分享",
			imageUrl:bannerList.value[0].picurl
		}
	})
</script>

<style lang="scss" scoped>
.content {
	.banner {
		padding: 30rpx 0;
		swiper {
			width: 750rpx;
			height: 340rpx;
			padding: 0 30px;
			swiper-item {
				width: 100%;
				height: 100%;
				image {
					width: 100%;
					height: 100%;
					border-radius: 10rpx;
				}
			}
		}
	}
	.notice {
		width: 690rpx;
		height: 80rpx;
		margin: 0 auto;
		display: flex;
		border-radius: 80rpx;
		background-color: #e2deeb;
		line-height: 80rpx;
		.left {
			width: 140rpx;
			/* 居中操作 */
			display: flex;
			justify-content: center;
			align-items: center;
			.text {
				color: $text-font-color-1;
				font-weight: 600;
				font-size: 28rpx;
			}
		}
		.center {
			flex: 1;
			height: 100%;
			swiper {
				height: 100%;
				swiper-item,navigator {
					height: 100%;
					font-size: 30rpx;
					/* 单行文字多余省略号 */
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
				}
			}
		}
		.right {
			width: 70rpx;
			/* 居中操作 */
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
	.select {
		padding: 50rpx 0;
		.custom {
			color: #27b28c;
			display: flex;
			justify-content: center;
		}
		.content {
			width: 720rpx;
			margin-left: 30rpx;
			margin-top: 30rpx;
			scroll-view {
				white-space: nowrap;
				.box {
					width: 200rpx;
					height: 430rpx;
					display: inline-block;
					margin-right: 15rpx;
					image {
						width: 100%;
						height: 100%;
						border-radius: 20rpx;
					}
				}
				.box:last-child {
					margin-right: 30rpx;
				}
			}
		}
	}
	.theme {
		padding: 50rpx 0;
		.custom {
			color: #27b28c;
		}
		.content {
			margin-top: 30rpx;
			padding: 0 30rpx;
			display: grid;
			gap: 10rpx;
			grid-template-columns: repeat(3,1fr);
		}
	}
}
</style>
